package org.jeecg.modules.wlhy.bill.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ArrayListDict;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.ConvertUtil;
import org.jeecg.modules.paycenter.enums.BillTypeEnum;
import org.jeecg.modules.paycenter.service.BillService;
import org.jeecg.modules.wlhy.bill.dto.HyBillDTO;
import org.jeecg.modules.wlhy.bill.dto.HyBillShippingNoteDTO;
import org.jeecg.modules.wlhy.bill.entity.HyBill;
import org.jeecg.modules.wlhy.bill.service.IHyBillService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerConfig;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerConfigService;
import org.jeecg.modules.wlhy.hydriverbank.entity.HyDriverBank;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.DataMaskingUtil;
import org.jeecg.modules.wlhy.utils.HyCalcFeeUtil;
import org.jeecg.modules.wlhy.utils.HyStatusCheckUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags="hy_bill")
@RestController
@RequestMapping("/bill/hyBill")
@Slf4j
public class HyBillController extends JeecgController<HyBill, IHyBillService> {
	@Autowired
	private IHyBillService hyBillService;

	@Autowired
	private IHyShippingNoteService hyShippingNoteService;

	@Autowired
	private BillService billService;

	@Autowired
	private UserUtil userUtil;

	@Autowired
	private IHyOrderService hyOrderService;

	@Autowired
	private IHyCustomerConfigService customerConfigService;

	@Resource
	private IHySettingService settingService;

	private void setQueryWrapper(HyBillDTO billDTO , QueryWrapper<HyBillDTO> queryWrapper){
		if(StringUtils.isNotEmpty(billDTO.getOrderId())){
			queryWrapper.eq("a.order_id",billDTO.getOrderId());
		}
		if(StringUtils.isNotEmpty(billDTO.getCompanyId())){
			queryWrapper.eq("a.company_id",billDTO.getCompanyId());
		}
		if(StringUtils.isNotEmpty(billDTO.getShippingStatus())){
			queryWrapper.eq("a.shipping_status",billDTO.getShippingStatus());
		}
		String orderMode = billDTO.getOrderMode();
		if(StringUtils.isNotEmpty(orderMode)) {
			if(orderMode.contains(",")) {
				queryWrapper.in("a.order_mode", Arrays.asList(orderMode.split(",")));
			}else {
				queryWrapper.eq("a.order_mode", orderMode);
			}
		}
		String payChannel = billDTO.getPayChannel();
		if(StringUtils.isNotEmpty(payChannel)) {
			if(payChannel.contains(",")) {
				queryWrapper.in("a.pay_channel", Arrays.asList(payChannel.split(",")));
			}else {
				queryWrapper.eq("a.pay_channel", billDTO.getPayChannel());
			}
		}
		if(StringUtils.isNotEmpty(billDTO.getBillStatus())){
			queryWrapper.in("a.bill_status",Arrays.asList(billDTO.getBillStatus().split(",")));
		}

		if(StringUtils.isNotEmpty(billDTO.getShippingNoteNumber())){
			queryWrapper.like("a.shipping_note_number",billDTO.getShippingNoteNumber());
		}

		if(StringUtils.isNotEmpty(billDTO.getStatusDtReceive_begin())){
			queryWrapper.ge("a.status_dt_receive",billDTO.getStatusDtReceive_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(billDTO.getStatusDtReceive_end() )){
			queryWrapper.le("a.status_dt_receive",billDTO.getStatusDtReceive_end()+" 23:59:59" );
		}

		if(StringUtils.isNotEmpty(billDTO.getStatusDtLoading_begin())){
			queryWrapper.ge("a.status_dt_loading",billDTO.getStatusDtLoading_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(billDTO.getStatusDtLoading_end() )){
			queryWrapper.le("a.status_dt_loading",billDTO.getStatusDtLoading_end()+" 23:59:59" );
		}

		if(StringUtils.isNotEmpty(billDTO.getStatusDtUnloading_begin())){
			queryWrapper.ge("a.status_dt_unloading",billDTO.getStatusDtUnloading_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(billDTO.getStatusDtUnloading_end() )){
			queryWrapper.le("a.status_dt_unloading",billDTO.getStatusDtUnloading_end()+" 23:59:59" );
		}

		if(StringUtils.isNotEmpty(billDTO.getCarrierId())){
			queryWrapper.eq("a.carrier_id",billDTO.getCarrierId());
		}
		if(StringUtils.isNotEmpty(billDTO.getPayType())){
			queryWrapper.eq("a.pay_type",billDTO.getPayType());
		}


		if(StringUtils.isNotEmpty(billDTO.getDriverName()) ) {
			queryWrapper.like("d.driver_name", billDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(billDTO.getTelephone()) ) {
			queryWrapper.like("d.telephone", billDTO.getTelephone());
		}

		if(StringUtils.isNotEmpty(billDTO.getVehicleNumber())){
			queryWrapper.like("v.vehicle_number",billDTO.getVehicleNumber());
		}
	}

	//region 预付
	@GetMapping(value = "/getPreFeeReqList")
	public Result<?> getPreFeeReqList(HyBillDTO billDTO,
										@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",billDTO.getCustomerId());
			}
		}else {
			HyCustomer customer = userUtil.getCustomer();
			if(customer==null){
				throw new JeecgBootException("货主信息不存在!");
			}
			String izAgentData = billDTO.getIZAgentData();
			if ("1".equals(izAgentData)) {
				queryWrapper.eq("a.agent_id", customer.getId());
				if (StringUtils.isNotEmpty(customerId)) {
					queryWrapper.eq("a.customer_id", customerId);
				}
			} else {
				queryWrapper.eq("a.customer_id", customer.getId());
			}
		}
		queryWrapper.eq("a.pay_type", "3");
		queryWrapper.in("a.shipping_status", "3","4","5");
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.eq("a.pre_pay_status", billDTO.getPrePayStatus());
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillDTO> page = new Page<HyBillDTO>(pageNo, pageSize);
		IPage<HyBillDTO> pageList = hyBillService.getPreFeeReqList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@AutoLog(value = "hy_bill-批量创建预付支付订单")
	@ApiOperation(value="hy_bill-批量创建预付支付订单", notes="hy_bill-批量创建预付支付订单")
	@PostMapping(value = "reqPreFeeBill")
	@Transactional
	public Result<?> createPreFeeBill(@RequestBody JSONObject data) {
		String ids = data.getString("ids");
		if (StringUtils.isBlank(ids)) {
			return Result.error("ids不能为空");
		}
		List<String> shippingids = Arrays.asList(ids.split(","));
		if(shippingids.size() <=0){
			return Result.error("ids不能为空!");
		}

		boolean isSys = userUtil.isSys();
		String customerId = null;
		if (!isSys) {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				return Result.error("货主信息不存在");
			}
			customerId = customer.getId();
		}

		List<HyShippingNote> shippingNotes = hyShippingNoteService.listByIdsForUpdate(shippingids);
		if (shippingNotes == null || shippingNotes.size() == 0) {
			return Result.error("运单信息不能为空");
		}
		List<HyBill> hyBillList = hyBillService.getBillListByShippingIds(shippingids, BillTypeEnum.PRE_PAY.value);
		if (hyBillList.size() > 0) {
			return Result.error("所选运单已创建支付订单，请刷新后重试");
		}
		for (HyShippingNote shippingNote : shippingNotes) {
			if (!"3".equals(shippingNote.getPayType())) {
				return Result.error("不是预付运单,不能操作");
			}
			if (!isSys) {
				HyStatusCheckUtil.checkShippingNoteCustomerId(shippingNote, customerId);
			}
		}
		List<HyBill> hyBills = billService.createBills(shippingNotes, BillTypeEnum.PRE_PAY , null);
		this.hyBillService.saveBatch(hyBills);
		return Result.OK("创建成功!");
	}

	//endregion

	//region 分段预付
	@ApiOperation(value="运单表-支付关系表-分页列表查询", notes="运单表-支付关系表-分页列表查询")
	@GetMapping(value = "/reqSubFeeList")
	public Result<?> queryReqSubFeeList(HyBillDTO billDTO,
									   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		String shippingStatus = billDTO.getShippingStatus();
		if(StringUtils.isEmpty(shippingStatus)){
			return Result.error("shippingStatus不能为空");
		}
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setQueryWrapper(billDTO , queryWrapper);

		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(billDTO.getCustomerId())){
				queryWrapper.eq("a.customer_id",billDTO.getCustomerId());
			}
		}else {
			HyCustomer customer = userUtil.getCustomer();
			if(customer==null){
				throw new JeecgBootException("货主信息不存在!");
			}
			queryWrapper.eq("a.customer_id",customer.getId());
		}
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.isNull("b.id");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillDTO> page = new Page<HyBillDTO>(pageNo, pageSize);
		IPage<HyBillDTO> pageList = hyBillService.getReqSubFeeShippingPageList(page, queryWrapper, shippingStatus);
		return Result.OK(pageList);
	}

	@ApiOperation(value="运单表-支付关系表-分页列表查询", notes="运单表-支付关系表-分页列表查询")
	@GetMapping(value = "/subFeeList")
	public Result<?> querySubFeeList(HyBillDTO billDTO,
											@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setQueryWrapper(billDTO , queryWrapper);
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(billDTO.getCustomerId())){
				queryWrapper.eq("b.customer_id",billDTO.getCustomerId());
			}
		}else {
			HyCustomer customer = userUtil.getCustomer();
			if(customer==null){
				throw new JeecgBootException("货主信息不存在!");
			}
			queryWrapper.eq("b.customer_id",customer.getId());
		}
		if(StringUtils.isNotEmpty(billDTO.getPayStatus())){
			queryWrapper.eq("b.pay_status",billDTO.getPayStatus());
		}
		queryWrapper.eq("b.bill_type","7");
		queryWrapper.eq("b.del_flag", "0");
		queryWrapper.isNotNull("b.id");
		queryWrapper.orderByDesc("b.create_time");


		Page<HyBillDTO> page = new Page<HyBillDTO>(pageNo, pageSize);
		IPage<HyBillDTO> pageList = hyBillService.getBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@AutoLog(value = "hy_bill-批量申请预付订单")
	@ApiOperation(value="hy_bill-批量申请预付订单", notes="hy_bill-批量申请预付订单")
	@PostMapping(value = "/reqSubFee")
	@Transactional
	public Result<?> reqSubFee(@RequestBody JSONObject data) {
		String ids = data.getString("ids");
		if(StringUtils.isBlank(ids)){
			return Result.error("ids不能为空");
		}
		List<String> shippingids = Arrays.asList(ids.split(","));
		if(shippingids.size() <=0){
			return Result.error("ids不能为空!");
		}

		String shippingStatus = data.getString("shippingStatus");
		if(StringUtils.isBlank(shippingStatus)){
			return Result.error("shippingStatus不能为空");
		}
		String samt = data.getString("amt");
		if(!ConvertUtil.isMoney(samt)){
			return Result.error("金额格式错误");
		}
		boolean isSys = userUtil.isSys();
		String customerId = null;
		if(!isSys) {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				return Result.error("货主信息不存在");
			}
			customerId = customer.getId();
		}

		List<HyShippingNote> shippingNotes = hyShippingNoteService.listByIdsForUpdate(shippingids);
		if(shippingNotes == null || shippingNotes.size() == 0){
			return Result.error("运单信息不能为空");
		}
		List<HyBill> hyBillList = hyBillService.getBillListByShippingIds(shippingids, BillTypeEnum.SUB_PAY.value);
		if (hyBillList.size() > 0) {
			return Result.error("所选运单已创建支付订单，请刷新后重试");
		}

		BigDecimal amt = new BigDecimal(samt);
		for (HyShippingNote shippingNote : shippingNotes) {
			if(!StringUtils.equals(shippingStatus , shippingNote.getShippingStatus())){
				return Result.error(shippingNote.getShippingNoteNumber() + "运单状态错误");
			}
			if(!isSys){
				if(!StringUtils.equals(customerId,shippingNote.getCustomerId())){
					return Result.error("运单信息错误");
				}
			}
			//可预付的运费 不能超过 预估运费 = 装货量 * 运费单价
			BigDecimal ygCostPay = HyCalcFeeUtil.getYGCostPay(shippingNote);
			if(ygCostPay != null){
				//已分段预付的运费
				BigDecimal shippingSubFee = hyBillService.getShippingSubFee(shippingNote.getId());
				BigDecimal preFee = shippingNote.getPreFee();
				if(preFee == null){
					preFee = BigDecimal.ZERO;
				}
				BigDecimal shippingpreFeeTotal =  amt.add(shippingSubFee).add(preFee);
				if(shippingpreFeeTotal.compareTo(ygCostPay) >= 0){
					return Result.error(shippingNote.getShippingNoteNumber() + "超过实付运费金额，不能创建预付订单");
				}
			}
		}
		List<HyBill> hyBills = billService.createBills(shippingNotes, BillTypeEnum.SUB_PAY , amt);
		this.hyBillService.saveBatch(hyBills);
		return Result.OK("创建成功!");
	}

	//endregion

	//region 支付计划

	@ApiOperation(value="运单表-支付计划申请-分页列表查询", notes="运单表-支付计划申请-分页列表查询")
	@GetMapping(value = "/planFeeReqList")
	public Result<?> queryPlanFeeReqList(HyBillDTO billDTO,
										@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		String billType = billDTO.getBillType();
		if(StringUtils.isEmpty(billType)){
			return Result.error("billType不能为空");
		}
		if(!"8".equals(billType) && !"9".equals(billType) ){
			return Result.error("billType错误");
		}
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		}else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			String izAgentData = billDTO.getIZAgentData();
			if ("1".equals(izAgentData)) {
				queryWrapper.eq("a.agent_id", customer.getId());
				if (StringUtils.isNotEmpty(customerId)) {
					queryWrapper.eq("a.customer_id", customerId);
				}
			} else {
				queryWrapper.eq("a.customer_id", customer.getId());
			}
		}
		queryWrapper.eq("a.iz_used_pay_plan","1");
		if("8".equals(billType)){
			queryWrapper.ne("a.cost_pay1",0);
			queryWrapper.ne("a.shipping_status","0");
		}
		if("9".equals(billType)) {
			queryWrapper.ne("a.cost_pay2", 0);
//			queryWrapper.ne("a.shipping_status","0");
			queryWrapper.eq("a.shipping_status", "5");
//			queryWrapper.eq("a.shipping_pay_status","5");
			queryWrapper.notIn("a.shipping_pay_status", "0", "9", "10");
		}
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.isNull("b.id");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillDTO> page = new Page<HyBillDTO>(pageNo, pageSize);
		IPage<HyBillDTO> pageList = hyBillService.getPlanFeeReqPageList(page, queryWrapper, billType);
		return Result.OK(pageList);
	}


	@AutoLog(value = "hy_bill-批量创建支付计划订单")
	@ApiOperation(value="hy_bill-批量创建支付计划订单", notes="hy_bill-批量创建支付计划订单")
	@PostMapping(value = "reqPlanFee")
	@Transactional
	public Result<?> reqPlanFee(@RequestBody JSONObject data) {
		String ids = data.getString("ids");
		if (StringUtils.isBlank(ids)) {
			return Result.error("ids不能为空");
		}
		String billType = data.getString("billType");
		if (StringUtils.isBlank(billType)) {
			return Result.error("billType不能为空");
		}
		if (!"8".equals(billType) && !"9".equals(billType)) {
			return Result.error("billType错误");
		}

		boolean isSys = userUtil.isSys();
		String customerId = null;
		if (!isSys) {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				return Result.error("货主信息不存在");
			}
			customerId = customer.getId();
		}

		List<String> shippingids = Arrays.asList(ids.split(","));
		if(shippingids.size() <=0){
			return Result.error("ids不能为空!");
		}
		List<HyShippingNote> shippingNotes = hyShippingNoteService.listByIdsForUpdate(shippingids);
		if (shippingNotes == null || shippingNotes.size() == 0) {
			return Result.error("运单信息不能为空");
		}
		List<HyBill> hyBillList = hyBillService.getBillListByShippingIds(shippingids, billType);
		if (hyBillList.size() > 0) {
			return Result.error("所选运单已创建支付订单，请刷新后重试");
		}
		for (HyShippingNote shippingNote : shippingNotes) {
			if (!"1".equals(shippingNote.getIzUsedPayPlan())) {
				return Result.error("运单信息错误");
			}
			if (!isSys) {
				HyStatusCheckUtil.checkShippingNoteCustomerId(shippingNote, customerId);
			}
			if ("8".equals(billType)) {
				BigDecimal costPay1 = shippingNote.getCostPay1();
				if (costPay1 == null || BigDecimal.ZERO.compareTo(costPay1) >= 0) {
					return Result.error("运单首付运费信息错误");
				}
			}
			if ("9".equals(billType)) {
				BigDecimal costPay2 = shippingNote.getCostPay2();
				if (costPay2 == null || BigDecimal.ZERO.compareTo(costPay2) >= 0) {
					return Result.error("运单到付运费信息错误");
				}
			}
		}
		BillTypeEnum billTypeEnum = BillTypeEnum.getBillTypeEnum(billType);
		List<HyBill> hyBills = billService.createBills(shippingNotes, billTypeEnum , null);
		this.hyBillService.saveBatch(hyBills);
		return Result.OK("创建成功!");
	}

	@ApiOperation(value="运单表-支付计划订单-分页列表查询", notes="运单表-支付计划订单-分页列表查询")
	@GetMapping(value = "/planFeeList")
	public Result<?> planFeeList(HyBillDTO billDTO,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setQueryWrapper(billDTO , queryWrapper);
		String billType = billDTO.getBillType();
		if(StringUtils.isEmpty(billType)) {
			queryWrapper.in("b.bill_type", "8", "9");
		}else {
			if(!"8".equals(billType) && !"9".equals(billType) ){
				return Result.error("billType错误");
			}
			queryWrapper.eq("b.bill_type", billType);
		}
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		}else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			String izAgentData = billDTO.getIZAgentData();
			if ("1".equals(izAgentData)) {
				queryWrapper.eq("a.agent_id", customer.getId());
				if (StringUtils.isNotEmpty(customerId)) {
					queryWrapper.eq("a.customer_id", customerId);
				}
			} else {
				queryWrapper.eq("a.customer_id", customer.getId());
			}
		}
		if(StringUtils.isNotEmpty(billDTO.getPayStatus())){
			queryWrapper.eq("b.pay_status",billDTO.getPayStatus());
		}

		queryWrapper.eq("b.del_flag", "0");
		queryWrapper.isNotNull("b.id");
		queryWrapper.orderByDesc("b.create_time");


		Page<HyBillDTO> page = new Page<HyBillDTO>(pageNo, pageSize);
		IPage<HyBillDTO> pageList = hyBillService.getBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}
	//endregion

	//region bill
	private void setBillQueryWrapper(HyBillDTO billDTO , QueryWrapper<HyBillDTO> queryWrapper) {
		if (StringUtils.isNotEmpty(billDTO.getBillType())) {
			queryWrapper.eq("a.bill_type", billDTO.getBillType());
		}
		if (StringUtils.isNotEmpty(billDTO.getOrderId())) {
			queryWrapper.eq("b.order_id", billDTO.getOrderId());
		}
		if (StringUtils.isNotEmpty(billDTO.getCompanyId())) {
			queryWrapper.eq("b.company_id", billDTO.getCompanyId());
		}
		if (StringUtils.isNotEmpty(billDTO.getShippingStatus())) {
			queryWrapper.eq("b.shipping_status", billDTO.getShippingStatus());
		}
		if (StringUtils.isNotEmpty(billDTO.getDescriptionOfGoods())) {
			queryWrapper.eq("b.description_of_goods", billDTO.getDescriptionOfGoods());
		}
		String orderMode = billDTO.getOrderMode();
		if (StringUtils.isNotEmpty(orderMode)) {
			if (orderMode.contains(",")) {
				queryWrapper.in("b.order_mode", Arrays.asList(orderMode.split(",")));
			} else {
				queryWrapper.eq("b.order_mode", orderMode);
			}
		}
		String payChannel = billDTO.getPayChannel();
		if (StringUtils.isNotEmpty(payChannel)) {
			if(StringUtils.equals(payChannel.charAt(0)+"","!")){
				queryWrapper.ne("a.pay_channel", payChannel);
			}else {
				if (payChannel.contains(",")) {
					queryWrapper.in("a.pay_channel", Arrays.asList(payChannel.split(",")));
				} else {
					queryWrapper.eq("a.pay_channel", payChannel);
				}
			}
		}

		if (StringUtils.isNotEmpty(billDTO.getBillStatus())) {
			queryWrapper.in("a.bill_status", Arrays.asList(billDTO.getBillStatus().split(",")));
		}

		if (StringUtils.isNotEmpty(billDTO.getShippingNoteNumber())) {
			queryWrapper.like("b.shipping_note_number", billDTO.getShippingNoteNumber());
		}

		//Added at 2023-09-04 for 运单结算页面中新增路线查询
		if (StringUtils.isNotEmpty(billDTO.getSendAddressName())) {
			queryWrapper.like("b.send_address_name", billDTO.getSendAddressName());
		}
		if (StringUtils.isNotEmpty(billDTO.getReceiveAddressName())) {
			queryWrapper.like("b.receive_address_name", billDTO.getReceiveAddressName());
		}

		if (StringUtils.isNotEmpty(billDTO.getStatusDtReceive_begin())) {
			queryWrapper.ge("b.status_dt_receive", billDTO.getStatusDtReceive_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(billDTO.getStatusDtReceive_end())) {
			queryWrapper.le("b.status_dt_receive", billDTO.getStatusDtReceive_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(billDTO.getStatusDtLoading_begin())) {
			queryWrapper.ge("b.status_dt_loading", billDTO.getStatusDtLoading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(billDTO.getStatusDtLoading_end())) {
			queryWrapper.le("b.status_dt_loading", billDTO.getStatusDtLoading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(billDTO.getStatusDtUnloading_begin())) {
			queryWrapper.ge("b.status_dt_unloading", billDTO.getStatusDtUnloading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(billDTO.getStatusDtUnloading_end())) {
			queryWrapper.le("b.status_dt_unloading", billDTO.getStatusDtUnloading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(billDTO.getCarrierId())) {
			queryWrapper.eq("b.carrier_id", billDTO.getCarrierId());
		}
		if (StringUtils.isNotEmpty(billDTO.getPayType())) {
			queryWrapper.eq("b.pay_type", billDTO.getPayType());
		}
		if (StringUtils.isNotEmpty(billDTO.getDriverName())) {
			queryWrapper.like("d.driver_name", billDTO.getDriverName());
		}
		if (StringUtils.isNotEmpty(billDTO.getTelephone())) {
			queryWrapper.like("d.telephone", billDTO.getTelephone());
		}

		if (StringUtils.isNotEmpty(billDTO.getVehicleNumber())) {
			queryWrapper.like("v.vehicle_number", billDTO.getVehicleNumber());
		}

		if (StringUtils.isNotEmpty(billDTO.getDfId())) {
			queryWrapper.eq("a.df_id", billDTO.getDfId());
		}

		if (StringUtils.isNotEmpty(billDTO.getPayRecordId())) {
			queryWrapper.eq("a.pay_record_id", billDTO.getPayRecordId());
		}

		if (StringUtils.isNotEmpty(billDTO.getPayDtCheck_begin())) {
			queryWrapper.ge("b.pay_dt_check", billDTO.getPayDtCheck_begin() );
		}
		if (StringUtils.isNotEmpty(billDTO.getPayDtCheck_end())) {
			queryWrapper.le("b.pay_dt_check", billDTO.getPayDtCheck_end() );
		}

		if (StringUtils.isNotEmpty(billDTO.getAutDt_begin())) {
			queryWrapper.ge("a.aut_dt", billDTO.getAutDt_begin() );
		}
		if (StringUtils.isNotEmpty(billDTO.getAutDt_end())) {
			queryWrapper.le("a.aut_dt", billDTO.getAutDt_end() );
		}
	}

	@ApiOperation(value="支付订单表--分页列表查询", notes="支付订单表--分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryList(HyBillDTO billDTO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillShippingNoteDTO> page = new Page<HyBillShippingNoteDTO>(pageNo, pageSize);
		IPage<HyBillShippingNoteDTO> pageList = hyBillService.queryBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@ApiOperation(value="支付订单表--分页列表查询", notes="支付订单表--分页列表查询")
	@GetMapping(value = "/billList")
	public Result<?> queryBillList(HyBillDTO billDTO,
										 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		} else if (userUtil.isCustomerAdmin()) {
			queryWrapper.in("a.customer_id", userUtil.getCustomerAdminRelationIdList());
		} else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			queryWrapper.eq("a.customer_id", customer.getId());
		}
		String payStatus = billDTO.getPayStatus();
		if(StringUtils.isNotEmpty(payStatus)){
			queryWrapper.eq("a.pay_status", payStatus);
		}
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillShippingNoteDTO> page = new Page<HyBillShippingNoteDTO>(pageNo, pageSize);
		IPage<HyBillShippingNoteDTO> pageList = hyBillService.queryBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@ApiOperation(value="支付订单表--待支付分页列表查询", notes="支付订单表--待支付分页列表查询")
	@GetMapping(value = "/billPayList")
	public Result<?> queryBillPayList(HyBillDTO billDTO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		} else if (userUtil.isCustomerAdmin()) {
			queryWrapper.in("a.customer_id", userUtil.getCustomerAdminRelationIdList());
		} else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			queryWrapper.eq("a.customer_id", customer.getId());
		}

		//Added at 2023-09-11 for 货源号搜索,货源号搜索时的前提是没有货源id搜索条件
		if (StringUtils.isEmpty(billDTO.getOrderId()) && StringUtils.isNotEmpty(billDTO.getOrderNum())) {
			List<HyOrder> hyOrderList = this.hyOrderService.list(new LambdaQueryWrapper<HyOrder>().like(HyOrder::getOrderNum, billDTO.getOrderNum()));
			if (CollUtil.isNotEmpty(hyOrderList)) {
				List<String> ids = hyOrderList.stream().map(HyOrder::getId).collect(Collectors.toList());
				queryWrapper.in("b.order_id", ids);
			} else {
				return Result.OK();
			}
		}

		queryWrapper.eq("a.pay_status", "4");
		if (StringUtils.isNotEmpty(billDTO.getKhdh())){
			queryWrapper.eq("b.khdh",billDTO.getKhdh());
		}
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("b.status_dt_unloading");

		Page<HyBillShippingNoteDTO> page = new Page<HyBillShippingNoteDTO>(pageNo, pageSize);
		IPage<HyBillShippingNoteDTO> pageList = hyBillService.queryBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@ApiOperation(value="支付订单表--支付失败分页列表查询", notes="支付订单表--支付失败分页列表查询")
	@GetMapping(value = "/billPayFailList")
	public Result<?> queryBillPayFailList(HyBillDTO billDTO,
										  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		} else if (userUtil.isCustomerAdmin()) {
			queryWrapper.in("a.customer_id", userUtil.getCustomerAdminRelationIdList());
		} else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			queryWrapper.eq("a.customer_id", customer.getId());
		}
		queryWrapper.eq("a.pay_status", "7");
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillShippingNoteDTO> page = new Page<HyBillShippingNoteDTO>(pageNo, pageSize);
		IPage<HyBillShippingNoteDTO> pageList = hyBillService.queryBillPageList(page, queryWrapper);

		String dataMaskingFlag = this.settingService.getSettingValueByTag("data_masking");
		if("1".equals(dataMaskingFlag)) {
			for (HyBillShippingNoteDTO dto : pageList.getRecords()) {
				dto.setTelephone(DataMaskingUtil.phoneMasking(dto.getTelephone()));
				dto.setSjsfzh(DataMaskingUtil.idcardMasking(dto.getSjsfzh()));
				dto.setSjyhkh(DataMaskingUtil.bankNumberMasking(dto.getSjyhkh()));
			}
		}

		return Result.OK(pageList);
	}

	@ApiOperation(value="支付订单表--分页列表查询", notes="支付订单表--分页列表查询")
	@GetMapping(value = "/agentBillList")
	public Result<?> queryAgentBillList(HyBillDTO billDTO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		} else if (userUtil.isCustomerAdmin()) {
			queryWrapper.in("a.customer_id", userUtil.getCustomerAdminRelationIdList());
		} else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			queryWrapper.eq("a.agent_id", customer.getId());
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		}
		String payStatus = billDTO.getPayStatus();
		if(StringUtils.isNotEmpty(payStatus)){
			queryWrapper.eq("a.pay_status", payStatus);
		}
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillShippingNoteDTO> page = new Page<HyBillShippingNoteDTO>(pageNo, pageSize);
		IPage<HyBillShippingNoteDTO> pageList = hyBillService.queryBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@ApiOperation(value="支付订单表--经纪人分页列表查询", notes="支付订单表--经纪人分页列表查询")
	@GetMapping(value = "/agentBillPayList")
	public Result<?> queryAgentPayBillList(HyBillDTO billDTO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		HyCustomer customer = userUtil.getCustomer();
		if (customer == null) {
			throw new JeecgBootException("货主信息不存在!");
		}
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(StringUtils.isNotEmpty(customerId)){
			queryWrapper.eq("a.customer_id",customerId);
		}
		queryWrapper.eq("a.agent_id", customer.getId());
		queryWrapper.eq("a.pay_status", "4");
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillShippingNoteDTO> page = new Page<HyBillShippingNoteDTO>(pageNo, pageSize);
		IPage<HyBillShippingNoteDTO> pageList = hyBillService.queryBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@ApiOperation(value="支付订单表--经纪人支付失败分页列表查询", notes="支付订单表--经纪人支付失败分页列表查询")
	@GetMapping(value = "/agentBillPayFailList")
	public Result<?> queryAgentBillPayFailList(HyBillDTO billDTO,
										  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		HyCustomer customer = userUtil.getCustomer();
		if (customer == null) {
			throw new JeecgBootException("货主信息不存在!");
		}
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(StringUtils.isNotEmpty(customerId)){
			queryWrapper.eq("a.customer_id",customerId);
		}
		queryWrapper.eq("a.agent_id", customer.getId());
		queryWrapper.eq("a.pay_status", "7");
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");

		Page<HyBillShippingNoteDTO> page = new Page<HyBillShippingNoteDTO>(pageNo, pageSize);
		IPage<HyBillShippingNoteDTO> pageList = hyBillService.queryBillPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	//endregion

	//region base

	@ApiOperation(value="hy_bill-分页列表查询", notes="hy_bill-分页列表查询")
	@GetMapping(value = "/getShippingNoteBillList")
	@ArrayListDict
	public Result<?> getShippingNoteBillList(HyBill hyBill,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		String shippingId = hyBill.getShippingId();
		if(StringUtils.isEmpty(shippingId)){
			return Result.error("运单id不能为空");
		}
		QueryWrapper<HyBill> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("shipping_id",shippingId);
		queryWrapper.orderByAsc("pay_dt");
//		queryWrapper.orderByAsc("create_time");
		List<HyBill> list = hyBillService.list(queryWrapper);
		return Result.OK(list);
	}

	//	@ApiOperation(value="hy_bill-分页列表查询", notes="hy_bill-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<?> queryPageList(HyBill hyBill,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<HyBill> queryWrapper = QueryGenerator.initQueryWrapper(hyBill, req.getParameterMap());
//		Page<HyBill> page = new Page<HyBill>(pageNo, pageSize);
//		IPage<HyBill> pageList = hyBillService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}


	@AutoLog(value = "hy_bill-编辑")
	@ApiOperation(value="hy_bill-编辑", notes="hy_bill-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyBill hyBill) {
		if(true){
			//todo
			return Result.error("todo");
		}
		HyBill old = hyBillService.getById(hyBill.getId());
		if(old == null){
			return Result.error("数据不存在");
		}
		if(!"4".equals(old.getPayStatus())){
			return Result.error("不能编辑");
		}
		if(!userUtil.isSys()){
			HyCustomer customer = userUtil.getCustomer();
			if(customer == null){
				return Result.error("货主信息不存在");
			}
			if(!StringUtils.equals(old.getCustomerId() , customer.getId())){
				return Result.error("数据错误");
			}
		}
		old.setAmt(hyBill.getAmt());
		hyBillService.updateById(old);
		return Result.OK("编辑成功!");
	}

	@AutoLog(value = "hy_bill-通过id删除")
	@ApiOperation(value="hy_bill-通过id删除", notes="hy_bill-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		HyBill old = hyBillService.getById(id);
		if(old == null){
			return Result.error("数据不存在");
		}

		if(!"4".equals(old.getPayStatus())){
			return Result.error("不能删除");
		}

		if(!userUtil.isSys()){
			HyCustomer customer = userUtil.getCustomer();
			if(customer == null){
				return Result.error("货主信息不存在");
			}
			HyStatusCheckUtil.checkBillCustomerId(old,customer.getId());
		}
		hyBillService.removeById(id);
		return Result.OK("删除成功!");
	}


//	@AutoLog(value = "hy_bill-批量删除")
//	@ApiOperation(value="hy_bill-批量删除", notes="hy_bill-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.hyBillService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}

	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HyBillDTO billDTO) {
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		HyCustomer customer = null;
		HyCustomerConfig customerConfig=null;
		String role="";// 1 货主

		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		}else {
			 customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			role="1";
			 customerConfig = customerConfigService.getByCustomerId(customer.getId());

//			if(customerConfig!=null) {
//				if (!"1".equals(customerConfig.getIzShowDrvierFee())) {
//					queryWrapper.ne("hcb.bill_type","8");
//					queryWrapper.ne("hcb.bill_type","9");
//				}
//			}
			queryWrapper.eq("a.customer_id", customer.getId());
		}
		queryWrapper.eq("a.pay_status", "4");
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");


		String selections = request.getParameter("selections");
		if(StringUtils.isNotEmpty(selections)){
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("a.id", selectionList);
		}

		List<HyBillShippingNoteDTO> list = hyBillService.getBillShippingNoteList(queryWrapper);


		for (HyBillShippingNoteDTO hyBillShippingNoteDTO : list) {
			if ("1".equals(role)) {
				if(customerConfig!=null) {
					if (!"1".equals(customerConfig.getIzShowDrvierFee())) {
						if (hyBillShippingNoteDTO.getOrderMode().equals("3")) {
							hyBillShippingNoteDTO.setCostService(new BigDecimal(0));
							hyBillShippingNoteDTO.setCustomerFee(new BigDecimal(0));
							hyBillShippingNoteDTO.setGoodsWeightLoading(new BigDecimal(0));
							hyBillShippingNoteDTO.setGoodsWeightUnloading(new BigDecimal(0));
							hyBillShippingNoteDTO.setShippingUnitPrice(new BigDecimal(0));
							hyBillShippingNoteDTO.setCostPay(new BigDecimal(0));
							hyBillShippingNoteDTO.setPreUnitFee(new BigDecimal(0));
							hyBillShippingNoteDTO.setPreFee(new BigDecimal(0));
							hyBillShippingNoteDTO.setCostPayShipping(new BigDecimal(0));
							hyBillShippingNoteDTO.setDriverFee(new BigDecimal(0));
							hyBillShippingNoteDTO.setCostPayTotal(new BigDecimal(0));
							hyBillShippingNoteDTO.setCustomerFee(new BigDecimal(0));
							hyBillShippingNoteDTO.setAgentCostPay(new BigDecimal(0));
							hyBillShippingNoteDTO.setAgentCostPayShipping(new BigDecimal(0));
							hyBillShippingNoteDTO.setGasRechargeAmount(new BigDecimal(0));
							hyBillShippingNoteDTO.setCostPay1(new BigDecimal(0));
							hyBillShippingNoteDTO.setCostPay2(new BigDecimal(0));
							hyBillShippingNoteDTO.setDriverAmt(new BigDecimal(0));
							hyBillShippingNoteDTO.setCarrierAmt(new BigDecimal(0));

						}
					}
				}
			}
		}

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String title="支付订单表";
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, HyBillShippingNoteDTO.class);

		//按选择列导出
		String selCols = request.getParameter("selCols");
		if("1".equals(selCols)) {
			String field = request.getParameter("field");
			if (StringUtils.isNotEmpty(field)) {
				List<String> fields = Arrays.asList(field.split(","));
				List<String> selectCols = new ArrayList<>();
				for (String s : fields) {
					if (StringUtils.isNotEmpty(s)) {
						int idx = s.indexOf("_dictText");
						if (idx > -1) {
							s = s.substring(0, idx);
						}
						selectCols.add(s);
					}
				}
				mv.addObject(NormalExcelConstants.EXPORT_FIELDS, StringUtils.join(selectCols, ","));
			}
		}
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}

	@RequestMapping(value = "/exportXls2")
	public ModelAndView exportXls2(HttpServletRequest request, HyBillDTO billDTO) {
		QueryWrapper<HyBillDTO> queryWrapper = new QueryWrapper();
		setBillQueryWrapper(billDTO , queryWrapper);
		String customerId = billDTO.getCustomerId();
		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(customerId)){
				queryWrapper.eq("a.customer_id",customerId);
			}
		}else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			queryWrapper.eq("a.customer_id", customer.getId());
		}
		queryWrapper.eq("a.pay_status", "3");
		queryWrapper.eq("a.del_flag", "0");
		queryWrapper.orderByDesc("a.create_time");


		String selections = request.getParameter("selections");
		if(StringUtils.isNotEmpty(selections)){
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("a.id", selectionList);
		}

		List<HyBillShippingNoteDTO> list = hyBillService.getBillShippingNoteList(queryWrapper);

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String title="支付订单表";
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, HyBillShippingNoteDTO.class);

		//按选择列导出
		String selCols = request.getParameter("selCols");
		if("1".equals(selCols)) {
			String field = request.getParameter("field");
			if (StringUtils.isNotEmpty(field)) {
				List<String> fields = Arrays.asList(field.split(","));
				List<String> selectCols = new ArrayList<>();
				for (String s : fields) {
					if (StringUtils.isNotEmpty(s)) {
						int idx = s.indexOf("_dictText");
						if (idx > -1) {
							s = s.substring(0, idx);
						}
						selectCols.add(s);
					}
				}
				mv.addObject(NormalExcelConstants.EXPORT_FIELDS, StringUtils.join(selectCols, ","));
			}
		}
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}

	//endregion

	//region 审核

	@AutoLog(value = "HyBill-批量审核")
	@ApiOperation(value="HyBill-批量审核", notes="HyBill-批量审核")
	@PostMapping(value = "/autBatch")
	@RequiresPermissions("shipping:batchSysAut")
	public Result<?> autBatch(@RequestParam(name="ids",required=true) String ids) {
		if(StringUtils.isEmpty(ids)){
			return Result.error("未对账完成,不能审核!");
		}
		String realName = UserUtil.getLoginUser().getRName();
		List<HyBill> hyBills = hyBillService.listByIds(Arrays.asList(ids.split(",")));
		List<HyShippingNote> hyShippingNotes = new ArrayList<>();
		for (HyBill hyBill : hyBills) {
			if (!"3".equals(hyBill.getPayStatus())) {
				return Result.error("未对账完成,不能审核!");
			}
			hyBill.setPayStatus("4");
			hyBill.setAutUser(realName);//设置审核人
			hyBill.setAutDt(new Date()); //审核完成时间
			//直接开始支付
			HyShippingNote hyShippingNote = hyShippingNoteService.getById(hyBill.getShippingId());
			hyShippingNote.setShippingPayStatus("4");
			hyShippingNote.setPayDtAut(new Date());//申请支付时间
			hyShippingNotes.add(hyShippingNote);
		}
		hyShippingNoteService.updateBatchById(hyShippingNotes);
		hyBillService.updateBatchById(hyBills);
		return Result.OK("批量审核成功!");
	}

	//endregion
	/**
	 * 根据运单号上的司机id获取司机的银行卡信息
	 */
	@GetMapping("/getDriverBank")
	public Result<?> getDriverBank(@RequestParam(name="driverId") String driverId){

		HyDriverBank result = hyBillService.getDriverBankByDriverId(driverId);
		if (result!=null&&!result.equals("")){
			return Result.OK(result);
		}else {
			return Result.error("该司机未绑定银行卡");
		}
	}

	@AutoLog(value = "HyBill-审核")
	@ApiOperation(value="HyBill-审核", notes="HyBill-审核")
	@PutMapping(value = "/audit")
	@RequiresPermissions("shipping:sysAut")
	public Result<?> audit(@RequestParam(name="id") String id) {
		if(StringUtils.isEmpty(id)){
			return Result.error("未对账完成,不能审核!");
		}
		String realName = UserUtil.getLoginUser().getRName();
		HyBill hyBill = hyBillService.getById(id);
		if (!hyBill.getPayStatus().equals("3")){
			return Result.error("未对账完成,不能审核!");
		}else {
			hyBill.setPayStatus("4");
			hyBill.setAutUser(realName);//设置审核人
			hyBill.setAutDt(new Date()); //审核完成时间
			HyShippingNote hyShippingNote = hyShippingNoteService.getById(hyBill.getShippingId());
			hyShippingNote.setShippingPayStatus("4");
			hyShippingNote.setPayDtAut(new Date());//申请支付时间
			hyShippingNoteService.updateById(hyShippingNote);
		}
		hyBillService.updateById(hyBill);
		return Result.OK("审核成功");
	}
}
